package com.example.fourtestapp.ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.fourtestapp.R;
import com.example.fourtestapp.service.MilieuService;

public class MilieuSettingActivity extends AppCompatActivity {

    private SharedPreferences sp;
    private SharedPreferences.Editor ed;

    private Switch switchAuto;
    private EditText tempText;
    private EditText sdText;
    private EditText lightText;
    private EditText co2Text;
    private EditText pmText;
    private EditText roadText;
    private Button saveYuZhi;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_milieu_setting);
        sp = getSharedPreferences("milieuYuZhi", 0);
        ed = sp.edit();

        initView();
        initListener();
        if (!"".equals(sp.getString("auto","")) && sp.getString("auto","").equals("true")){
            switchAuto.setChecked(true);
        }
    }

    private void initView() {
        switchAuto = findViewById(R.id.switch_auto);
        tempText = findViewById(R.id.temp_text);
        sdText = findViewById(R.id.sd_text);
        lightText = findViewById(R.id.light_text);
        co2Text = findViewById(R.id.co2_text);
        pmText = findViewById(R.id.pm_text);
        roadText = findViewById(R.id.road_text);
        saveYuZhi = findViewById(R.id.save_yuZhi);
    }

    private void initListener() {

        saveYuZhi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ed.putString("temp", tempText.getText().toString());
                ed.putString("sd", sdText.getText().toString());
                ed.putString("light", lightText.getText().toString());
                ed.putString("co2", co2Text.getText().toString());
                ed.putString("pm", pmText.getText().toString());
                ed.putString("road", roadText.getText().toString());
                ed.commit();
            }
        });
        switchAuto.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                Intent intent = new Intent(MilieuSettingActivity.this, MilieuService.class);
                if (b) {
                    if (!sp.getString("temp", "").equals("") || !sp.getString("sd", "").equals("") ||
                            !sp.getString("light", "").equals("") || !sp.getString("co2", "").equals("") ||
                            !sp.getString("pm", "").equals("") || !sp.getString("road", "").equals("")) {
                        ed.putString("auto","true");
                        ed.commit();
                        startService(intent);
                    }else {
                        Toast.makeText(MilieuSettingActivity.this,"某项阈值为设置!",Toast.LENGTH_SHORT).show();
                        switchAuto.setChecked(false);
                    }
                }else {
                    stopService(intent);
                }
            }
        });

    }
}
